package com.lhd.qd.module.system.employee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhd.qd.common.base.BaseServiceImpl;
import com.lhd.qd.common.consts.dict.DictTypeCodeEnum;
import com.lhd.qd.common.translator.DBTransRule;
import com.lhd.qd.common.translator.DictTransRule;
import com.lhd.qd.common.translator.TransUtils;
import com.lhd.qd.module.system.dept.dao.DeptMapper;
import com.lhd.qd.module.system.employee.dao.EmployeeMapper;
import com.lhd.qd.module.system.employee.model.converter.AbstractEmployeeConverter;
import com.lhd.qd.module.system.employee.model.dataobj.EmployeeDO;
import com.lhd.qd.module.system.employee.model.dto.EmployeePageQuery;
import com.lhd.qd.module.system.employee.model.dto.EmployeeSaveDTO;
import com.lhd.qd.module.system.employee.model.vo.EmployeeDetailVO;
import com.lhd.qd.module.system.employee.model.vo.EmployeeListVO;
import com.lhd.qd.module.system.employee.service.EmployeeService;
import com.lhd.qd.module.system.hospital.dao.HospitalMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<EmployeeMapper, EmployeeDO> implements EmployeeService {

    @Override
    public IPage<EmployeeListVO> pageEmployee(EmployeePageQuery query) {

        LambdaQueryWrapper<EmployeeDO> queryWrapper = Wrappers.<EmployeeDO>lambdaQuery()
                .like(StringUtils.isNotEmpty(query.getName()), EmployeeDO::getName, query.getName())
                .eq(query.getDeptId() != null, EmployeeDO::getDeptId, query.getDeptId())
                .eq(query.getHospitalId() != null, EmployeeDO::getHospitalId, query.getHospitalId());

        IPage<EmployeeDO> doPage = page(new Page<>(query.getCurrent(), query.getSize()), queryWrapper);

        IPage<EmployeeListVO> voPage = AbstractEmployeeConverter.INSTANCE.doPage2ListVOPage(doPage);

        TransUtils.trans(voPage.getRecords(), EmployeeListVO.class, new DictTransRule[]{
                new DictTransRule(DictTypeCodeEnum.GENDER, "genderCode")
        });

        trans(voPage.getRecords(), EmployeeListVO.class);

        return voPage;
    }

    private <T> void trans(List<T> entityList, Class<T> entityType) {
        TransUtils.trans(entityList, entityType, new DBTransRule[]{
                new DBTransRule<>(
                        new String[]{"hospitalId", "hospitalName"},
                        HospitalMapper.class,
                        new String[]{"id", "name"}
                ),
                new DBTransRule<>(
                        new String[]{"deptId", "deptName"},
                        DeptMapper.class,
                        new String[]{"id", "name"}
                ),
        });
    }

    @Override
    public EmployeeDetailVO getEmployeeById(Long id) {

        EmployeeDetailVO vo = AbstractEmployeeConverter.INSTANCE.do2DetailVO(getById(id));

        trans(Collections.singletonList(vo), EmployeeDetailVO.class);

        return vo;
    }

    @Override
    public boolean saveEmployee(EmployeeSaveDTO saveDTO) {

        EmployeeDO dataObj = AbstractEmployeeConverter.INSTANCE.saveDTO2DO(saveDTO);

        return save(dataObj);
    }

    @Override
    public boolean updateEmployeeById(Long id, EmployeeSaveDTO saveDTO) {

        EmployeeDO dataObj = AbstractEmployeeConverter.INSTANCE.saveDTO2DO(saveDTO);

        dataObj.setId(id);

        return updateById(dataObj);
    }

    @Override
    public boolean removeEmployeeById(Long id) {

        EmployeeDO dataObj = new EmployeeDO();
        dataObj.setId(id);
        return removeByIdWithFill(dataObj);
    }
}
